package com.iwhalecloud.bss.kite.cucc.service.pos.handler;

import com.iwhalecloud.bss.kite.cart.service.shoppingcart.loader.impl.KiteCartSceneInstLoader;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.ord.pos.vo.KitePosOfferInstVO;
import com.iwhalecloud.bss.kite.service.ord.pos.vo.KitePosProdInstVO;
import com.iwhalecloud.bss.kite.service.util.KiteInstUtils;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 极简受理转单场景业务对象辅助工具类
 *
 * @author xiao.xuzhang<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2019年9月22日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.service.ord.pos.handler <br>
 */
public class KitePosSceneUtils {
    /**
     * 根据实例ID生成转单销售品实例对象
     */
    public static KitePosOfferInstVO generateKitePosOfferInstVOByInstId(String sceneInstId, String cartId, String custId, String instId) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KitePosOfferInstVO instVo = new KitePosOfferInstVO();
        KiteOfferInst kiteOfferInst = querySceneInstService.getOfferInst(custId, instId);

        if (KiteObjectUtils.isEmpty(kiteOfferInst)) {
            KiteCartSceneInstLoader kiteCartSceneInstLoader = SpringUtil.getBean(KiteCartSceneInstLoader.class);
            kiteOfferInst = (KiteOfferInst) kiteCartSceneInstLoader.loadInst(cartId, custId, sceneInstId, instId);
        }

        instVo.setOfferInst(kiteOfferInst);
        return instVo;
    }

    /**
     * 根据实例ID生成转单主产品实例对象
     */
    public static KitePosProdInstVO generateKitePosProdInstVOByInstId(String sceneInstId, String cartId, String custId, String instId) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KitePosProdInstVO instVo = new KitePosProdInstVO();
        KiteProdInst kiteProdInst = querySceneInstService.getProdInst(custId, instId);

        if (KiteObjectUtils.isEmpty(kiteProdInst)) {
            KiteCartSceneInstLoader kiteCartSceneInstLoader = SpringUtil.getBean(KiteCartSceneInstLoader.class);
            kiteProdInst = (KiteProdInst) kiteCartSceneInstLoader.loadInst(cartId, custId, sceneInstId, instId);
        }

        String roleId = KiteInstUtils.getOfferObjInstRelRoleIdByActionType(sceneInstId, instId, custId, kiteProdInst.getActionType());
        if (KiteStringUtils.isNotEmpty(roleId)) {
            kiteProdInst.setRoleId(roleId);
        }
        instVo.setProdInst(kiteProdInst);
        return instVo;
    }

}
